package com.quanshu.exchange.ui.gui.profile;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.TextView;

import com.baselibrary.util.glide.GlideUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.exchange.ui.gui.qrcode.utils.QrUtils;
import com.quanshu.shop.base.view.BaseTitleBar;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.quanshu.exchange.support.utils.Util.bmpToByteArray;

public class InviteRegisterActivity extends BaseActivity {
    @BindView(R.id.qr_image)
    ImageView qrImage;
    @BindView(R.id.avatar)
    ImageView avatar;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.invite_title)
    BaseTitleBar inviteTitle;
    @BindView(R.id.desc)
    TextView desc;

    private Bitmap mBitmap;
    private IWXAPI iwxapi;
    private Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_register_qrcode);
        ButterKnife.bind(this);
        mContext = this;
        iwxapi = WXAPIFactory.createWXAPI(this, MyApplication.getInstance().getWX_ID(), false);
        initView();
        initListener();
    }

    private void initView() {
        ImmersionBar.with(this).titleBar(inviteTitle).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        String url = Config.URL_HOST_SHOP + "cattle_exchange_show/sRegister?" + currentUser.getMemberid();
        mBitmap = QrUtils.createImage(url, 1000, 1000, null);
        qrImage.setImageBitmap(mBitmap);
        name.setText(currentUser.getNickname());
        GlideUtils.setRoundImageView(this, currentUser.getAvatar(), R.drawable.default_avatar, avatar);
        inviteTitle.setCenterText("邀请注册").setLeftImage(R.drawable.icon_back_black_2_0).setRightText("分享").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {
                DialogShare dialogShare = new DialogShare(mContext);
                dialogShare.show();
                //分享到微信
                dialogShare.getWeChatSubject().subscribe(o -> {
                    doShareWeChat("weChat");
                    dialogShare.cancel();
                });
                //分享到朋友圈
                dialogShare.getFriendsSubject().subscribe(o -> {
                    doShareWeChat("friends");
                    dialogShare.cancel();
                });
            }
        });
    }

    private void initListener() {

    }

    private void doShareWeChat(String type) {
        WXImageObject wxImageObject = new WXImageObject(mBitmap);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = wxImageObject;
        //设置缩略图
        Bitmap mBp = Bitmap.createScaledBitmap(mBitmap, 120, 120, true);
        msg.thumbData = bmpToByteArray(mBp, true);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");//  transaction字段用
        req.message = msg;
        if (type.equals("weChat")) {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        } else if (type.equals("friends")) {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        }
        iwxapi.sendReq(req);
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    @Override
    protected void onDestroy() {
        if (mBitmap != null && mBitmap.isRecycled()) {
            mBitmap.recycle();
            mBitmap = null;
        }
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
